import { Button, Form, Input, message } from 'antd';
import { useNavigate } from "react-router-dom";
import { userlogin } from '../request/api';
import '../assets/css/login.css';

function Login() {
    let Navigate = useNavigate();

    const onFinish = (values) => {
        userlogin(values).then(res => {
            if (res.code == 200) {
                sessionStorage.setItem('userinfo', JSON.stringify(res.list));
                message.success(res.msg);
                Navigate('/index/menu');
                return;
            }
            message.warning(res.msg);
        })
    };

    return (
        <div className='login'>
            <Form
                layout="vertical"
                className='form'
                onFinish={onFinish}
            >
                <Form.Item
                    label="用户名"
                    name="username"
                    rules={[
                        {
                            required: true,
                            message: '请输入用户名!',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="password"
                    rules={[
                        {
                            required: true,
                            message: '请输入密码!',
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>
                <Form.Item
                    wrapperCol={{
                        offset: 8,
                        span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit">
                        登录
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}

export default Login;